﻿namespace Pub.Class
{
    using System;
    using System.IO.Compression;
    using System.Runtime.CompilerServices;
    using System.Web;

    public static class HttpContextExtensions
    {
        public static void Cache(this HttpContext instance, TimeSpan duration)
        {
            instance.CheckOnNull("instance");
            HttpCachePolicy cache = instance.Response.Cache;
            cache.SetCacheability(HttpCacheability.Public);
            cache.SetOmitVaryStar(true);
            cache.SetExpires(instance.Timestamp.Add(duration));
            cache.SetMaxAge(duration);
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(instance.Timestamp);
            cache.SetLastModifiedFromFileDependencies();
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        }

        public static void CacheKD(this HttpContext instance, int durationInMinutes)
        {
            instance.CheckOnNull("instance");
            TimeSpan span = TimeSpan.FromMinutes((double) durationInMinutes);
            HttpCachePolicy cache = instance.Response.Cache;
            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(span));
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            cache.SetMaxAge(span);
        }

        public static void Compress(this HttpContext instance)
        {
            instance.CheckOnNull("instance");
            HttpRequest request = instance.Request;
            if ((request.Browser.MajorVersion >= 7) || !request.Browser.IsBrowser("IE"))
            {
                if (instance.IsEncodingAccepted("gzip"))
                {
                    instance.Response.Filter = new GZipStream(instance.Response.Filter, CompressionMode.Compress);
                    instance.SetEncoding("gzip");
                }
                else if (instance.IsEncodingAccepted("deflate"))
                {
                    instance.Response.Filter = new DeflateStream(instance.Response.Filter, CompressionMode.Compress);
                    instance.SetEncoding("deflate");
                }
            }
        }

        public static bool IsEncodingAccepted(this HttpContext instance, string encoding)
        {
            if (instance.IsNull())
            {
                return false;
            }
            return (instance.Request.Headers["Accept-encoding"].IsNotNull() && instance.Request.Headers["Accept-encoding"].Contains(encoding));
        }

        public static void NoCache(this HttpContext instance)
        {
            instance.Response.Cache.SetNoServerCaching();
            instance.Response.Cache.SetNoStore();
            instance.Response.Cache.SetMaxAge(TimeSpan.Zero);
            instance.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            instance.Response.Cache.SetExpires(DateTime.Now.AddYears(-1));
        }

        public static void SetEncoding(this HttpContext instance, string encoding)
        {
            instance.Response.AppendHeader("Content-encoding", encoding);
        }
    }
}

